from lxml import etree
import requests
import pandas as db


url = "https://www.jkl.com.cn/shop.aspx"
agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0"

html = requests.get(url).text
element = etree.HTML(html)
c_urls = element.xpath('//div[@class="infoLis"]//li/a/@href')

all_data = db.DataFrame()

for c_url in c_urls:
    # https://www.jkl.com.cn/shopLis.aspx?current=2&TypeId=10045
    # https://www.jkl.com.cn/shopLis.aspx?TypeId=10045
    # https://www.jkl.com.cn/shopLis.aspx?TypeId=10044
    page = 1
    while True:
        s_url = "https://www.jkl.com.cn/" + c_url + "&" +f"current={page}"
        print(s_url)
        c_html = requests.get(s_url).text
        c_element = etree.HTML(c_html)
        name = c_element.xpath('//span[@class="con01"]/text()')
        address = c_element.xpath('//span[@class="con02"]/text()')
        phon = c_element.xpath('//span[@class="con03"]/text()')
        time = c_element.xpath('//span[@class="con04"]/text()')
        end_page = c_element.xpath('//a[text()="尾页"]/@href')
        data = db.DataFrame({
            "店铺名称":name,
            "店铺地址": address,
            "电话" : phon,
            "营业时间": time
        })
        print(end_page)
        all_data = db.concat([all_data, data])
        if end_page == []:
            break
        page += 1

all_data.to_csv(
    "./file/店铺信息.csv",  # 路径
    index=False,  # 不保存行索引（默认True，会多一列0,1,2...）
    encoding="ANSI",  # 编码格式（避免中文乱码）
    sep=",",  # 分隔符（默认逗号，可改成制表符\t等）
)

